package ncu.me.musicplayer.sql;


import java.util.List;

import ncu.me.musicplayer.MyApplication;
import ncu.me.musicplayer.entity.SearchItem;
import ncu.me.musicplayer.entity.SearchItemDao;

/**
 * @Author ye
 * @Date 2020/6/2 20:29
 * @Version 1.0
 */
public class SearchDao {
    SearchItemDao mDao;
    private SearchDao(){
        mDao = MyApplication.sDaoSession.getSearchItemDao();
    }
    public static SearchDao getInstance(){return SearchDao.Holder.instance;}
    static class Holder{
        static SearchDao instance = new SearchDao();
    }
    public List<SearchItem> getAll(){
        return mDao.loadAll();
    }
    public List<SearchItem> query(String key){
         return mDao.queryBuilder()
                 .where(SearchItemDao.Properties.Key.like("%"+key+"%"))
                 .limit(20)
                 .orderDesc(SearchItemDao.Properties.Time)
                 .list();
    }
    public void insert(SearchItem item){
        try{
            if (isHas(item.getKey())){
                mDao.save(item);
            }else {
                mDao.insert(item);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public void update(SearchItem item){
        mDao.update(item);
    }
    public void delete(SearchItem item){
        mDao.delete(item);
    }
    public boolean isHas(String key){
        return mDao.queryBuilder().where(SearchItemDao.Properties.Key.eq(key)).count()>0;
    }
}
